package com.wss.module.wan.ui.series.mvp;

import com.orhanobut.logger.Logger;
import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.utils.JsonUtils;
import com.wss.common.utils.ValidUtils;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.ui.series.mvp.contract.SeriesArticleContract;
import com.wss.module.wan.ui.series.mvp.model.SeriesArticleModel;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Describe：体系文章Presenter
 * Created by 吴天强 on 2018/11/15.
 */
public class SeriesArticlePresenter extends BasePresenter<SeriesArticleModel, SeriesArticleContract.View>
        implements SeriesArticleContract.Presenter {


    @Override
    public void getArticle() {

        Logger.e("==== get article .....");

        showLoading();
        getModel().getArticle(getView().getPage(), getView().getSeriesId(), new SimpleCallBack<String>() {
            @Override
            public void onError(ApiException e) {
                getView().onError("", e.getMessage());
            }

            @Override
            public void onSuccess(String string) {
                dismissLoading();

                String data = JsonUtils.getString(string, "data");
                List<Article> list = JsonUtils.getList(JsonUtils.getString(data, "datas"), Article.class);

                if (ValidUtils.isValid(list)) {
                    getView().refreshArticleList(list);
                } else {
                    getView().onEmpty("");
                }
            }
        });
    }


    @Override
    protected SeriesArticleModel createModule() {
        return new SeriesArticleModel(getLifecycleOwner());
    }

    @Override
    public void start() {
        getArticle();
    }
}
